﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace CarRental.Controls
{
    using CarRental.Database;
    using System.Data;

    public partial class CarEdit : System.Web.UI.UserControl
    {
        public String ExitLocation { get; set; }

        protected void Page_Load(object sender, EventArgs e)
        {
            Page.Form.Action = "/Admin/CarEdit.aspx";

            if (Request.QueryString["id"] != null) 
            {
                int carId = Int32.Parse(Request.QueryString["id"]);
                var car = Database.GetCar(carId);

                Page.Form.Action += "?id=" + car.Id;

                hidId.Value = car.Id.ToString();

                if (!IsPostBack)
                {
                    txtRegistrationNumber.Text = car.RegistrationNumber;

                    ddProducer.DataSource = GetProducers();
                    ddProducer.DataTextField = "ProducerName";
                    ddProducer.DataValueField = "Id";
                    ddProducer.DataBind();
                    ddProducer.Items.FindByValue(car.Producer.Id.ToString()).Selected = true;

                    ddModel.DataSource = GetModels(car.Producer.Id);
                    ddModel.DataTextField = "ModelName";
                    ddModel.DataValueField = "Id";
                    ddModel.DataBind();
                    ddModel.Items.FindByValue(car.Model.Id.ToString()).Selected = true;

                    txtProductionYear.Text = car.ProductionYear.ToString("dd-MM-yyyy");

                    chkIsActive.Checked = car.IsActive;
                }
            }
        }

        private DataTable GetProducers()
        {
            var producers = Database.GetProducers();

            DataTable dataTable = new DataTable();

            // - Dodanie definicji kolumn -

            dataTable.Columns.Add(new DataColumn("Id", typeof(int)));
            dataTable.Columns.Add(new DataColumn("ProducerName", typeof(string)));

            foreach (var row in producers)
            {
                DataRow dataRow = dataTable.NewRow();

                dataRow["Id"] = row.Id;
                dataRow["ProducerName"] = row.ProducerName;

                dataTable.Rows.Add(dataRow);
            }

            return dataTable;
        }

        private DataTable GetModels(int ProducerId)
        {
            var models = Database.GetModels(ProducerId);

            DataTable dataTable = new DataTable();

            // - Dodanie definicji kolumn -

            dataTable.Columns.Add(new DataColumn("Id", typeof(int)));
            dataTable.Columns.Add(new DataColumn("ModelName", typeof(string)));

            foreach (var row in models)
            {
                DataRow dataRow = dataTable.NewRow();

                dataRow["Id"] = row.Id;
                dataRow["ModelName"] = row.ModelName;

                dataTable.Rows.Add(dataRow);
            }

            return dataTable;
        }

        protected void SaveForm(object sender, EventArgs e)
        {
            int Id = Int32.Parse(hidId.Value);
            int ModelId = Int32.Parse(ddModel.SelectedValue);
            String RegistrationNumber = txtRegistrationNumber.Text;
            DateTime ProductionYear = DateTime.Parse(txtProductionYear.Text);
            bool IsActive = chkIsActive.Checked;

            Database.EditCar(Id, RegistrationNumber, ProductionYear, ModelId, IsActive);

            InfoMessage.InnerHtml = "Zapisano." + " <a href=\"" + ExitLocation + "\">Powróć</a>";
            InfoMessage.Visible = true;
        }

        protected void CancelForm(object sender, EventArgs e)
        {
            if (this.ExitLocation != String.Empty)
                Response.Redirect(this.ExitLocation);
        }

        protected void ProducerChange(object sender, EventArgs e)
        {
            ddModel.DataSource = GetModels(Int32.Parse(ddProducer.SelectedValue));
            ddModel.DataTextField = "ModelName";
            ddModel.DataValueField = "Id";
            ddModel.DataBind();
        }
    }
}